
local table = table
local surface = surface
local draw = draw
local math = math
local string = string

local function clr( color ) return color.r, color.g, color.b, color.a end

surface.CreateFont("Default", 40, 100, true, true, "Value1")
surface.CreateFont("Default", 20, 100, true, true, "Value2")

surface.CreateFont("Default", 40, 100, true, false, "Time")

surface.CreateFont("Default", 25, 100, true, false, "HUDText1")

local vars =
{
	padding = 5,
	margin = 10,

	bar_spacing = 5,
 
	bar_height = 32,
 
	width = 0.25
};

local colors =
{
	theme =
	{
		background = Color(0, 0, 0, 175),
		fill = Color(200, 200, 200, 255),
		shade = Color(0, 0, 0, 50),
		text = Color(0, 0, 0, 255)
	};
	theme_zombie =
	{
		background = Color(0, 0, 0, 175),
		fill = Color(209, 45, 45, 255),
		shade = Color(0, 0, 0, 50),
		text = Color(0, 0, 0, 255)
	}
};

local state_string = 
{
	[STATE_WAIT] = "Waiting for players.",
	[STATE_PREP] = "Preparing for round.",
	[STATE_ACTIVE] = "Started round.",
	[STATE_POST] = "Round ended."
};

local function PaintBar(x, y, w, h, colors, value)
	draw.RoundedBox(6, x, y, w, h, colors.background)
	
	local width = w * math.Clamp(value, 0, 1)
	
	if width > 0 then
		-- draw.RoundedBox(2, x + 1, y + 1, width - 2, h - 2, colors.shade)
		-- draw.RoundedBox(1, x + 2, y + 2, width - 4, h - 4, colors.fill)
		draw.RoundedBox(6, x, y, width, h, colors.fill)
	end
end

local function DrawInfo(x, y, s, colors, value, value2, background, font)
	if background then
		draw.RoundedBox(4, x, y, s, s, colors.background)
	end
	
	surface.SetFont(font)
	surface.SetTextPos(x + s/15, y - s/20)
	surface.SetTextColor(clr(colors.fill))
	surface.DrawText(value)
	
	if value2 then
		surface.SetFont("Value2")
		surface.SetTextPos(x + s/15, y + 30)
		surface.SetTextColor(clr(colors.fill))
		surface.DrawText(value2)
	end
end

local function DrawTextBox(x, y, w, h, colors, text)
	-- draw.RoundedBox(4, x, y, w, h, colors.background)
	
	surface.SetFont("HUDText1")
	surface.SetTextPos(x + 5, y - 3)
	surface.SetTextColor(clr(colors.text))
	surface.DrawText(text)
end

local function DrawImage(x, y, w, h, tex, color)
	surface.SetDrawColor(clr(color))
	surface.SetTexture(tex)
	surface.DrawTexturedRect(x, y, w, h)
end

local function Background()
	local w = 350
	local h = 87
	
	local height = h
	
	local x = 0
	local y = ScrH() - h
	
	draw.RoundedBox(4, x, y, w, h, Color(0, 0, 0, 175))
end

local Health = 0
local MaxHealth = 0
local function HealthBar()
	local client = LocalPlayer()

	local width = 320
	local bar_width = width - vars.padding
	local height = vars.padding + vars.bar_height + vars.bar_spacing
	
	local x = vars.margin
	local y = ScrH() - vars.margin - height
	
	local cx = x + vars.padding
	local cy = y + vars.padding
	
	if client:IsHuman() then MaxHealth = 100 elseif client:IsZombie() then MaxHealth = 5000 end
	
	if client:IsHuman() or client:IsZombie() then
		if client:Health() > (MaxHealth * 0.02) then
			Health = client:Health()
		elseif client:Health() > 0 then
			Health = MaxHealth * 0.03
		else
			Health = 0
		end
	else
		Health = 100
		MaxHealth = 100
	end
	
	PaintBar(cx, cy, bar_width, vars.bar_height, theme, Health/MaxHealth)
end

local function Ammo()
	local client = LocalPlayer()
	
	if client:Alive() and client:GetActiveWeapon() then
	
		local mag_current = 0
		local mag_stored = 0
		
		if client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) > 2000 then
			mag_current = client:GetActiveWeapon():Clip1()
			mag_stored = "UNLIM."
		else
			mag_current = client:GetActiveWeapon():Clip1()
			mag_stored = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType())
		end
			
		local s = 64
			
		local height = vars.padding + s + 5
			
		local x = ScrW() - vars.margin - s
		local y = ScrH() - vars.margin - height
			
		local cx = x - vars.padding
		local cy = y + vars.padding
			
		DrawInfo(cx, cy, s, theme, mag_current, mag_stored, true, "Value1")
	end
end

local function State()
	local client = LocalPlayer()

	if GAMEMODE.state < 3 or GAMEMODE.state > 3 then
		state = state_string[GAMEMODE.state]
	else
		state = team.GetName(client:Team())
	end
	
	local w = 64
	local h = 32
	
	local height = vars.padding + vars.bar_height
	
	local x = vars.margin
	local y = ScrH() - vars.margin - height
	
	local cx = x + vars.padding
	local cy = y + vars.padding
	
	DrawTextBox(cx, cy, w, h, theme, state)
end

local function Timer()
	local time = string.FormattedTime(math.max(0, GetGlobalFloat("ze_round_end", 0) - CurTime()), "%02i:%02i")
	
	local s = 64
	
	local height = vars.padding + vars.bar_height * 2.075
	
	local x = vars.margin - 4.5
	local y = ScrH() - vars.margin - height
	
	local cx = x + vars.padding
	local cy = y + vars.padding
	
	DrawInfo(cx, cy, s, theme, time, false, false, "Time")
end

local function Blood()
	local bloodsplat = surface.GetTextureID("HUD/bloodsplat")
	
	local s = 256
	
	local height = s
	
	local x = 0
	local y = ScrH() - s
	
	DrawImage(x, y, s, s, bloodsplat, Color(200, 100, 100, 180))
end

function GM:HUDPaint()
	local client = LocalPlayer()
	
	GAMEMODE:HUDDrawTargetID()
	GAMEMODE:HUDDrawPickupHistory()
	
	MSTACK:Draw(client)
	
	Background()
	
	if client:IsHuman() or client:IsSpec() then
		theme = colors.theme
	else
		theme = colors.theme_zombie
	end
	
	if client:IsHuman() and client:Alive() and IsValid(client:GetActiveWeapon()) then
		Ammo()
	end
	
	Blood()
	
	HealthBar()
	State()
	
	if GAMEMODE.state > 1 then
		Timer()
	end
end

local hud = {"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}
function GM:HUDShouldDraw(name)
	for k, v in pairs(hud) do
		if name == v then return false end
	end

	return true
end

	
	